﻿namespace ACCOUNTING
{
    using ACCOUNTING.LVY.Business;
    using ACCOUNTING.My;
    using C1.Win.C1FlexGrid;
    using C1.Win.C1Input;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    [StandardModule]
    internal sealed class LVYModule
    {
        private static ThongTinCongTy objCompanyInfor = new ThongTinCongTy();
        private static string strPathConnect = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\DRAGON2\Connect.xml");
        private static string strPathXML = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\DRAGON2\LVY.xml");
        private static string strPathXMLxx = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\DRAGON2\LVYxx.xml");
        public static byte[] TheKey = new byte[8];
        private static byte[] Vector = new byte[] { 0x12, 0x44, 0x16, 0xee, 0x88, 0x15, 0xdd, 0x41 };

        public static void advFormatNumber(object objTextbox, string strFormat)
        {
            NewLateBinding.LateSet(objTextbox, null, "FormatType", new object[] { FormatTypeEnum.CustomFormat }, null, null);
            NewLateBinding.LateSet(objTextbox, null, "CustomFormat", new object[] { strFormat }, null, null);
        }

        public static void BuildComboHangMucBaoCaoThue(object objCombo)
        {
            try
            {
                string commandText = "";
                commandText = "SELECT Khoa,Ma,NoiDung FROM DM_HangMucBaoCaoThue WHERE LoaiPhatSinh = 'MV'";
                DataTable table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
                NewLateBinding.LateSet(objCombo, null, "DataSource", new object[] { table }, null, null);
                NewLateBinding.LateSet(objCombo, null, "ValueMember", new object[] { "Khoa" }, null, null);
                NewLateBinding.LateSet(objCombo, null, "DisplayMember", new object[] { "Ma" }, null, null);
                NewLateBinding.LateSet(objCombo, null, "ColumnWidths", new object[] { "0;60;200;0" }, null, null);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                ProjectData.ClearProjectError();
            }
        }

        public static void BuildComboLoaiHoaDon(object objCombo, string strConditions = "", string strOrder = "")
        {
            DataTable table = new clsDMLoaiHoaDon().ShowListByField("Khoa|Ma|Ten" + modGeneral.H_LANGUAGE + " As Ten|TyLeThue", strConditions, strOrder);
            NewLateBinding.LateSet(objCombo, null, "DataSource", new object[] { table }, null, null);
            NewLateBinding.LateSet(objCombo, null, "ValueMember", new object[] { "Khoa" }, null, null);
            NewLateBinding.LateSet(objCombo, null, "DisplayMember", new object[] { "Ma" }, null, null);
            NewLateBinding.LateSet(objCombo, null, "ColumnWidths", new object[] { "0;60;200;0" }, null, null);
        }

        public static string Char2Date(string str)
        {
            if (ReturnIfNull(str, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() == "")
            {
                return "";
            }
            return (sRight(str.Trim(), 2) + "/" + sMid(str.Trim(), 5, 2) + "/" + sLeft(str.Trim(), 4));
        }

        public static bool CheckConnection()
        {
            bool flag=false;
            if (ReturnIfNull(modGeneral.ConnectionString, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() == "")
            {
                if (modGeneral.H_LANGUAGE == "VIET")
                {
                    ShowWarning("Bạn phải thiết lập lại th\x00f4ng số kết nối đến database.");
                }
                else
                {
                    ShowWarning("You must setup connection to database's information again!");
                }
                return false;
            }
            try
            {
                OleDbConnection connection = new OleDbConnection(modGeneral.ConnectionString);
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    connection.Dispose();
                    return true;
                }
                if (modGeneral.H_LANGUAGE == "VIET")
                {
                    ShowWarning("Kết nối kh\x00f4ng th\x00e0nh c\x00f4ng! Bạn phải thiết lập lại th\x00f4ng số kết nối.");
                }
                else
                {
                    ShowWarning("It can not connect to database! Please try again.");
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ShowWarning("Kh\x00f4ng thể kết nối đến cơ sở dữ liệu !\r" + exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public static bool CheckDateLock(string pDate)
        {
            string commandText = " SELECT Ngay FROM HT_Lock";
            modGeneral.H_LOCKDATE = ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), "", modGeneral.TypeOfValue.Nothing).ToString().Trim();
            if ((ReturnIfNull(modGeneral.H_LOCKDATE, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() != "") && (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(pDate, modGeneral.H_LOCKDATE, false) <= 0))
            {
                ShowWarning(modGeneral.Tranlated("Ng\x00e0y chứng từ đ\x00e3 bị kh\x00f3a! Li\x00ean hệ người quản trị để biết th\x00eam chi tiết", ""));
                return false;
            }
            return true;
        }

        public static void CheckHangTonToiThieu()
        {
            string str = DateAndTime.Year(DateAndTime.Today).ToString().Trim() + sRight("00" + DateAndTime.Month(DateAndTime.Today).ToString().Trim(), 2);
            string commandText = "SELECT HH.Ma, HH.TenViet As TenHang,  DVT.TenViet As DonViTinh, HH.SoLuongTonToiThieu,   Sum(TK.SoLuongTonDau) + Sum(TK.SoLuongNhap) - Sum(TK.SoLuongXuat) As SoLuongTon  FROM DM_HangHoa HH  LEFT JOIN DM_DonViTinh DVT ON HH.KhoaDonViTinh = DVT.Khoa  LEFT JOIN ST_TonKhoDauKy TK ON HH.Khoa = TK.KhoaHangHoa And TK.NamThang = '" + str + "'  WHERE(HH.SoLuongTonToiThieu > 0) GROUP BY HH.Ma, HH.TenViet, HH.SoLuongTonToiThieu, DVT.TenViet  Having HH.SoLuongTonToiThieu >= Sum(TK.SoLuongTonDau) + Sum(TK.SoLuongNhap) - Sum(TK.SoLuongXuat) ";
            DataTable table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            if (table.Rows.Count > 0)
            {
                MyProject.Forms.Frm_CanhBaoHangTonKho.objData = table;
                MyProject.Forms.Frm_CanhBaoHangTonKho.ShowDialog();
            }
        }

        public static bool CheckValidCurrency(string strKhoaTaiKhoan, string strLoaiTien)
        {
            clsDMTaiKhoan khoan = new clsDMTaiKhoan();
            clsDMLoaiTien tien = new clsDMLoaiTien();
            khoan.Load(strKhoaTaiKhoan);
            if (khoan.LoaiTien.Trim() == "")
            {
                khoan.LoaiTien = modGeneral.H_CURRENCY;
            }
            if (khoan.LoaiTien.Trim() == strLoaiTien.Trim())
            {
                return true;
            }
            tien.LoadByCode(khoan.LoaiTien.Trim());
            if (tien.TienViet == 1)
            {
                return true;
            }
            ShowWarning(modGeneral.Tranlated("Loại tiền hạch to\x00e1n kh\x00f4ng hợp lệ với loại tiền được qui định đối với t\x00e0i khoản ", "") + khoan.SoTaiKhoan);
            return false;
        }

        public static void CloseFileLicense()
        {
            CreateKey("LVY.xml");
            EncryptFile(strPathXML, strPathXMLxx);
            File.Delete(strPathXML);
            MyProject.Computer.FileSystem.RenameFile(strPathXMLxx, "LVY.xml");
        }

        public static bool ConfirmDelete(string strMsg = "")
        {
            if (strMsg.Trim() == "")
            {
                strMsg = modGeneral.Tranlated("Bạn thật sự muốn x\x00f3a th\x00f4ng tin n\x00e0y!", "");
            }
            return (MessageBox.Show(strMsg, "DRAGON2 - Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes);
        }

        public static void CreateKey(string strKey)
        {
            int num2;
            byte[] bytes = new byte[8];
            ASCIIEncoding encoding = new ASCIIEncoding();
            int charIndex = 0;
            encoding.GetBytes(strKey, charIndex, strKey.Length, bytes, charIndex);
            byte[] buffer2 = new SHA1CryptoServiceProvider().ComputeHash(bytes);
            charIndex = 0;
            do
            {
                TheKey[charIndex] = buffer2[charIndex];
                charIndex++;
                num2 = 7;
            }
            while (charIndex <= num2);
        }

        public static string Date2Char(string str)
        {
            return (sRight(str, 4) + sMid(str, 4, 2) + sLeft(str, 2));
        }

        public static string Date2Letter(string strDate)
        {
            string str4 = "";
            string str2 = sLeft(strDate, 2);
            string str3 = sMid(strDate, 4, 2);
            string str5 = sRight(strDate.Trim(), 4);
            if (strDate.Trim() == "")
            {
                return "";
            }
            if (modGeneral.H_LANGUAGE.ToUpper().Trim() == "VIET")
            {
                str4 = "Ng\x00e0y " + str2 + " th\x00e1ng " + str3 + " năm " + str5;
            }
            else
            {
                str4 = str2 + " " + DateAndTime.MonthName(Conversions.ToInteger(str3), false) + " " + str5;
            }
            return str4;
        }

        public static string DateLetter(string strDate, string strLanguage = "VIET")
        {
            if (strLanguage.Trim().ToUpper() == "VIET")
            {
                return ("Ng\x00e0y " + sLeft(strDate, 2) + " th\x00e1ng " + sMid(strDate, 4, 2) + " năm " + sRight(strDate, 4));
            }
            if (strLanguage.Trim().ToUpper() == "ANH")
            {
                return (sLeft(strDate, 2) + " " + Month2LetterInEnglish(sMid(strDate, 4, 2)) + " " + sRight(strDate, 4));
            }
            return strDate;
        }

        public static void DecryptFile(string inName, string outName)
        {
            try
            {
                byte[] array = new byte[0x1001];
                long num2 = 8L;
                FileStream stream2 = new FileStream(inName, FileMode.Open, FileAccess.Read);
                FileStream stream3 = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                stream3.SetLength(0L);
                long length = stream2.Length;
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                CryptoStream stream = new CryptoStream(stream3, provider.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write);
                while (num2 < length)
                {
                    int count = stream2.Read(array, 0, 0x1000);
                    stream.Write(array, 0, count);
                    num2 = Convert.ToInt32((double) (num2 + ((((double) count) / ((double) provider.BlockSize)) * provider.BlockSize)));
                }
                stream.Close();
                stream2.Close();
                stream3.Close();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception2 = exception1;
                Interaction.MsgBox(exception2.Message + "Please ensure that you are using the correct password", MsgBoxStyle.OkOnly, null);
                ProjectData.ClearProjectError();
            }
        }

        public static void EncryptFile(string inName, string outName)
        {
            try
            {
                byte[] array = new byte[0x1001];
                long num2 = 8L;
                FileStream stream2 = new FileStream(inName, FileMode.Open, FileAccess.Read);
                FileStream stream3 = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                stream3.SetLength(0L);
                long length = stream2.Length;
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                CryptoStream stream = new CryptoStream(stream3, provider.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write);
                while (num2 < length)
                {
                    int count = stream2.Read(array, 0, 0x1000);
                    stream.Write(array, 0, count);
                    num2 = Convert.ToInt32((double) (num2 + ((((double) count) / ((double) provider.BlockSize)) * provider.BlockSize)));
                }
                stream.Close();
                stream2.Close();
                stream3.Close();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                Interaction.MsgBox(exception.Message, MsgBoxStyle.OkOnly, null);
                ProjectData.ClearProjectError();
            }
        }

        public static bool FormatDateInRegional()
        {
            if (MyProject.Computer.Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true).GetValue("sShortdate").ToString().Trim().ToUpper() != "DD/MM/YYYY")
            {
                string str = "dd/MM/yyyy";
                MyProject.Computer.Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true).SetValue("sShortdate", str);
                return true;
            }
            return true;
        }

        private static byte[] GetImageData(string filename)
        {
            FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(input);
            return reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length));
        }

        public static void GetUserRight(ref bool mView, ref bool mDel, ref bool mEdit, ref bool mAdd, string mMenuKey, string mUserName)
        {
            if (ReturnIfNull(mUserName, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() == "")
            {
                mView = false;
                mDel = false;
                mEdit = false;
                mAdd = false;
            }
            else
            {
                clsNguoiDung dung = new clsNguoiDung();
                mView = true;
                mDel = true;
                mEdit = true;
                mAdd = true;
                dung.Load(mUserName);
                if (dung.KhoaNhanVien != "000000000000000")
                {
                    DataTable right = dung.GetRight(mUserName, mMenuKey);
                    if (right.Rows.Count == 0)
                    {
                        mView = false;
                        mDel = false;
                        mEdit = false;
                        mAdd = false;
                    }
                    else
                    {
                        if (right.Rows[0]["F003"].ToString().Trim() == "1")
                        {
                            mView = true;
                        }
                        else
                        {
                            mView = false;
                        }
                        if (right.Rows[0]["F004"].ToString().Trim() == "1")
                        {
                            mAdd = true;
                        }
                        else
                        {
                            mAdd = false;
                        }
                        if (right.Rows[0]["F005"].ToString().Trim() == "1")
                        {
                            mEdit = true;
                        }
                        else
                        {
                            mEdit = false;
                        }
                        if (right.Rows[0]["F006"].ToString().Trim() == "1")
                        {
                            mDel = true;
                        }
                        else
                        {
                            mDel = false;
                        }
                    }
                }
            }
        }

        public static void InitConfig()
        {
            clsConfig config = new clsConfig();
            config.Load();
            modGeneral.H_LOAIGIA = config.PhuongPhapGiaTonKho;
            modGeneral.H_THAYDOICHUNGTU = config.ThayDoiChungTu;
            modGeneral.H_THEODOISERI = config.TheoDoiSoSeri > 0;
            modGeneral.H_THEODOIHANDUNG = config.TheoDoiHanSuDung > 0;
            modGeneral.H_THEODOISOLOHANG = config.TheoDoiSoLohang > 0;
            modGeneral.H_THEODOIQUAYKE = config.TheoDoiQuayKe > 0;
            modGeneral.H_STOCKID = config.KhoaKhoMacDinh;
            modGeneral.H_GHISODONGTHOI = config.GhiSoDongThoi > 0;
            modGeneral.H_GIABANBAOGOMTHUE = config.GiaBanBaoGomThue > 0;
            modGeneral.H_THEODOISERIALKHIXUAT = config.TheoDoiSoSeriKhiXuat > 0;
            modGeneral.H_KHONGXUATQUACONGNO = config.KhongXuatNoQuaDinhMuc > 0;
            modGeneral.H_CHOPHEPAMQUY = config.ChoPhepAmQuy > 0;
            if (modGeneral.H_THEODOISERI)
            {
                modGeneral.H_THEODOISERIALKHIXUAT = true;
            }
            modGeneral.H_CHOPHEPXUATAM = config.ChoPhepXuatAm > 0;
            modGeneral.H_FIRSTDAY = config.NgayBatDauSuDung();
            modGeneral.H_CHONHANGXUATTUDANHMUC = config.ChonHangXuatTuDanhMuc > 0;
            modGeneral.H_SOLIENIN = config.SoLienIn;
            modGeneral.H_LOAIINMACDINH = config.LoaiInMacDinh;
            modGeneral.H_LOAIHOADONINMACDINH = config.LoaiHoaDonInMacDinh;
            modGeneral.H_DANHAPTTCT = config.DaNhapTTCT;
            modGeneral.H_MAUHOADONMACDINH = config.MauHoaDonMacDinh;
            modGeneral.H_DONVICHUQUAN = config.DonViChuQuan;
            modGeneral.H_MSTDONVICHUQUAN = config.MSTDonViChuQuan;
            modGeneral.H_COQUANTHUE = config.CoQuanThue;
        }

        public static bool InitConnection()
        {
            modGeneral.H_KEYAPPNAME = modGeneral.MD5Encrypt("ERP");
            modGeneral.H_DBSERVERNAME = ReadConnectionStringFromXML("ServerName");
            modGeneral.H_DBDATANAME = ReadConnectionStringFromXML("DatabaseName");
            modGeneral.H_DBUSER = ReadConnectionStringFromXML("UserName");
            modGeneral.H_DBPASS = modGeneral.Decrypt(ReadConnectionStringFromXML("Password"));
            modGeneral.DBAccessType = modGeneral.enumDBAccessType.oledb;
            modGeneral.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + modGeneral.H_DBUSER + ";PWD=" + modGeneral.H_DBPASS + ";Initial Catalog=" + modGeneral.H_DBDATANAME + ";Data Source=" + modGeneral.H_DBSERVERNAME;
            return CheckConnection();
        }

        public static void InitDataCompany()
        {
            ThongTinCongTy ty = new ThongTinCongTy();
            ty.Load(modGeneral.H_CLIENT);
            modGeneral.H_DATACOMPANY = new ReportData.CompanyDataTable();
            DataRow row = modGeneral.H_DATACOMPANY.NewRow();
            row[0] = modGeneral.H_COMPANYNAMEVIET;
            row[1] = modGeneral.H_COMPANYNAMEANH;
            row[2] = modGeneral.H_ADDRESS;
            row[3] = modGeneral.H_TELEPHONE;
            row[4] = modGeneral.H_FAX;
            row[5] = modGeneral.H_TAXCODE;
            row[6] = ty.Logo;
            modGeneral.H_DATACOMPANY.Rows.Add(row);
        }

        public static void InitDataDefault()
        {
            DataTable table = new DefaultValue().ShowList("");
            if (table != null)
            {
                modGeneral.H_DEFAULT.Clear();
                int num2 = table.Rows.Count - 1;
                for (int i = 0; i <= num2; i++)
                {
                    modGeneral.H_DEFAULT.Add(Conversions.ToString(table.Rows[i]["Ma"]), Conversions.ToString(table.Rows[i]["NghiepVu"]), Conversions.ToString(table.Rows[i][2]), Conversions.ToString(table.Rows[i]["GiaTri"]));
                }
            }
        }

        public static void InitDataPhanBo()
        {
            modGeneral.H_PPPB = new DataTable();
            modGeneral.H_PPPB.Columns.Add("Ma", System.Type.GetType("System.String"));
            modGeneral.H_PPPB.Columns.Add("Ten", System.Type.GetType("System.String"));
            DataRow row = modGeneral.H_PPPB.NewRow();
            row[0] = "TG";
            row[1] = modGeneral.Tranlated("Trị gi\x00e1", "");
            modGeneral.H_PPPB.Rows.Add(row);
            row = modGeneral.H_PPPB.NewRow();
            row[0] = "SL";
            row[1] = modGeneral.Tranlated("Số lượng", "");
            modGeneral.H_PPPB.Rows.Add(row);
            row = modGeneral.H_PPPB.NewRow();
            row[0] = "PB";
            row[1] = modGeneral.Tranlated("Tự ph\x00e2n bổ", "");
            modGeneral.H_PPPB.Rows.Add(row);
        }

        public static void InitInformation()
        {
            objCompanyInfor.Load(modGeneral.H_CLIENT);
            modGeneral.H_COMPANYNAMEVIET = objCompanyInfor.TenCongTyViet;
            modGeneral.H_COMPANYNAMEANH = objCompanyInfor.TenCongTyAnh;
            modGeneral.H_ADDRESSVIET = objCompanyInfor.DiaChiViet;
            modGeneral.H_ADDRESSANH = objCompanyInfor.DiaChiAnh;
            modGeneral.H_ADDRESS = modGeneral.H_ADDRESSVIET;
            modGeneral.H_DCGIAOHANG = objCompanyInfor.DiaChiAnh;
            modGeneral.H_TELEPHONE = objCompanyInfor.DienThoai;
            modGeneral.H_FAX = objCompanyInfor.Fax;
            modGeneral.H_CURRENCY = "VND";
            modGeneral.H_EMAIL = objCompanyInfor.Email;
            modGeneral.H_WEBSITE = objCompanyInfor.Website;
            modGeneral.H_BANK = objCompanyInfor.TenNganHang;
            modGeneral.H_ACCOUNTNAME = objCompanyInfor.TenTaiKhoan;
            modGeneral.H_ACCOUNTNUMBERUSD = objCompanyInfor.SoTaiKhoanUSD;
            modGeneral.H_ACOUNTTNUMBERVND = objCompanyInfor.SoTaiKhoanVND;
            modGeneral.H_BANKADDESS = objCompanyInfor.DiaChiNganHang;
            modGeneral.H_SWIFTCODE = objCompanyInfor.SwiftCode;
            modGeneral.H_TAXCODE = objCompanyInfor.MaSoThue;
            modGeneral.H_HOTLINE = objCompanyInfor.HotLine;
            modGeneral.H_FORMATNUMBER = "###,###,###,###,###,##0";
            modGeneral.H_APPLOCATION = "CN";
            modGeneral.H_GIACHUAN = new clsDMLoaiGia().GetGiaChuan();
            modGeneral.H_PHIENBANQUYETDINH = objCompanyInfor.PhienBanQuyetDinh;
        }

        public static void InitProgram()
        {
            if (!Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(NewLateBinding.LateGet(ReturnIfNull(modGeneral.H_CLIENT, "", modGeneral.TypeOfValue.Nothing), null, "Trim", new object[0], null, null, null), "", false))
            {
                InitInformation();
                InitDataPhanBo();
                InitDataDefault();
                InitConfig();
                InitDataCompany();
            }
        }

        public static Hashtable MapDataDanhMuc(modGeneral.ListDanhMuc pDanhMuc)
        {
            int num;
            DataTable table;
            Hashtable hashtable2 = new Hashtable();
            modGeneral.ListDanhMuc muc = pDanhMuc;
            switch (muc)
            {
                case modGeneral.ListDanhMuc.QuocGia:
                {
                    table = new clsDMQuocGia().ShowList("");
                    int num2 = table.Rows.Count - 1;
                    for (num = 0; num <= num2; num++)
                    {
                        hashtable2.Add(RuntimeHelpers.GetObjectValue(table.Rows[num]["Khoa"]), RuntimeHelpers.GetObjectValue(table.Rows[num]["Ten"]));
                    }
                    return hashtable2;
                }
                case modGeneral.ListDanhMuc.HangSanXuat:
                {
                    table = new clsDMHangSanXuat().ShowList("", "");
                    int num3 = table.Rows.Count - 1;
                    for (num = 0; num <= num3; num++)
                    {
                        hashtable2.Add(RuntimeHelpers.GetObjectValue(table.Rows[num]["Khoa"]), RuntimeHelpers.GetObjectValue(table.Rows[num]["Ten"]));
                    }
                    return hashtable2;
                }
                case modGeneral.ListDanhMuc.KhoHang:
                {
                    table = new clsDMKho().ShowList("");
                    int num4 = table.Rows.Count - 1;
                    for (num = 0; num <= num4; num++)
                    {
                        hashtable2.Add(RuntimeHelpers.GetObjectValue(table.Rows[num]["Khoa"]), RuntimeHelpers.GetObjectValue(table.Rows[num]["Ten"]));
                    }
                    return hashtable2;
                }
                case modGeneral.ListDanhMuc.DonViTinh:
                {
                    table = new clsDMDonViTinh().ShowList("");
                    int num5 = table.Rows.Count - 1;
                    for (num = 0; num <= num5; num++)
                    {
                        hashtable2.Add(RuntimeHelpers.GetObjectValue(table.Rows[num]["Khoa"]), RuntimeHelpers.GetObjectValue(table.Rows[num]["Ten"]));
                    }
                    return hashtable2;
                }
            }
            if (muc == modGeneral.ListDanhMuc.LoaiIn)
            {
                table = new DefaultValue().ShowLoaiIn();
                int num6 = table.Rows.Count - 1;
                for (num = 0; num <= num6; num++)
                {
                    hashtable2.Add(RuntimeHelpers.GetObjectValue(table.Rows[num]["Khoa"]), RuntimeHelpers.GetObjectValue(table.Rows[num]["LoaiIn"]));
                }
            }
            return hashtable2;
        }

        public static string Month2LetterInEnglish(string strMonth)
        {
            string str=String.Empty;
            switch (strMonth)
            {
                case "01":
                    return "January";

                case "02":
                    return "February";

                case "03":
                    return "Match";

                case "04":
                    return "April";

                case "05":
                    return "May";

                case "06":
                    return "June";

                case "07":
                    return "July";

                case "08":
                    return "August";

                case "09":
                    return "September";

                case "10":
                    return "October";

                case "11":
                    return "November";

                case "12":
                    str = "December";
                    break;
            }
            return str;
        }

        public static string Month2Quarter(string strMonth)
        {
            string str3 = "";
            string str2 = "";
            str3 = sLeft(strMonth, 4);
            int num = Conversions.ToInteger(sRight(strMonth, 2));
            if ((num >= 1) & (num <= 3))
            {
                str2 = str3 + "01";
            }
            if ((num >= 4) & (num <= 6))
            {
                str2 = str3 + "02";
            }
            if ((num >= 7) & (num <= 9))
            {
                str2 = str3 + "03";
            }
            if ((num >= 10) & (num <= 12))
            {
                str2 = str3 + "04";
            }
            return str2;
        }

        public static string MonthAdd(int pMonth, int pYear, int pNumAdd)
        {
            int num = 0;
            int num2 = 0;
            if ((pMonth + pNumAdd) > 12)
            {
                pMonth = 12 - (pMonth + pNumAdd);
                num2 = pYear + 1;
            }
            else
            {
                num = pMonth + pNumAdd;
                num2 = pYear;
            }
            return (num2.ToString() + sRight("00" + num.ToString(), 2));
        }

        public static string MY2YM(string str)
        {
            if (str.Trim() == "")
            {
                return "";
            }
            return (sRight(str.Trim(), 4) + sLeft(str, 2));
        }

        public static void OpenFileLicense()
        {
            try
            {
                CreateKey("LVY.xml");
                DecryptFile(strPathXML, strPathXMLxx);
                File.Delete(strPathXML);
                MyProject.Computer.FileSystem.RenameFile(strPathXMLxx, "LVY.xml");
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ProjectData.ClearProjectError();
            }
        }

        public static string ReadConnectionStringFromXML(string Name)
        {
             IEnumerator enumerator=null;
            if (!File.Exists(strPathConnect))
            {
                string str=String.Empty;
                return str;
            }
            XmlDocument document = new XmlDocument();
            string innerText = "";
            document.Load(strPathConnect);
            XmlNodeList elementsByTagName = document.GetElementsByTagName(Name);
            try
            {
                enumerator = elementsByTagName.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    XmlNode current = (XmlNode) enumerator.Current;
                    innerText = current.InnerText;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return innerText;
        }

        public static string ReadValueElementFromXML(string FileName, string Name)
        {
             IEnumerator enumerator=null;
            if (!File.Exists(FileName))
            {
                string str=String.Empty;
                return str;
            }
            XmlDocument document = new XmlDocument();
            string innerText = "";
            document.Load(FileName);
            XmlNodeList elementsByTagName = document.GetElementsByTagName(Name);
            try
            {
                enumerator = elementsByTagName.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    XmlNode current = (XmlNode) enumerator.Current;
                    innerText = current.InnerText;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return innerText;
        }

        public static object ReturnIfNull(object pValue, object pValueReturn, modGeneral.TypeOfValue TypeOfValue = 0)
        {
            switch (((int) TypeOfValue))
            {
                case 0:
                    if (pValue != null)
                    {
                        if (pValue.ToString().Trim() == "")
                        {
                            return pValueReturn;
                        }
                        return pValue;
                    }
                    return pValueReturn;

                case 1:
                    return pValue;

                case 2:
                    if (pValue != null)
                    {
                        if (pValue.GetType().ToString() != System.Type.GetType("System.String").ToString())
                        {
                            return pValueReturn;
                        }
                        return pValue;
                    }
                    return pValueReturn;
            }
            return pValue;
        }

        public static void SaveConnectionStringToXML(string strServer, string strDatabase, string strUserName, string strPassword)
        {
            XmlDocument document = new XmlDocument();
            XmlTextWriter writer = new XmlTextWriter(strPathConnect, Encoding.UTF8) {
                Formatting = Formatting.Indented
            };
            writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
            writer.WriteStartElement("Setting");
            writer.WriteStartElement("ServerName");
            writer.WriteString(strServer);
            writer.WriteEndElement();
            writer.WriteStartElement("DatabaseName");
            writer.WriteString(strDatabase);
            writer.WriteEndElement();
            writer.WriteStartElement("UserName");
            writer.WriteString(strUserName);
            writer.WriteEndElement();
            writer.WriteStartElement("Password");
            writer.WriteString(modGeneral.Encrypt(strPassword));
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Close();
            document.Load(strPathConnect);
        }

        public static void SetColorForGrid(object pObjGrid, Color pColor, int nRow, int nCol1 = 0, int nRow2 = 0, int nCol2 = 0)
        {
            if (nRow2 == 0)
            {
                nRow2 = nRow;
            }
            if (nCol2 == 0)
            {
                nCol2 = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.SubtractObject(NewLateBinding.LateGet(NewLateBinding.LateGet(pObjGrid, null, "Cols", new object[0], null, null, null), null, "Count", new object[0], null, null, null), 1));
            }
            CellStyle style = (CellStyle) NewLateBinding.LateGet(NewLateBinding.LateGet(pObjGrid, null, "Styles", new object[0], null, null, null), null, "Add", new object[] { pColor.Name }, null, null, null);
            style.BackColor = pColor;
            object[] arguments = new object[] { nRow, nCol1, nRow2, nCol2 };
            bool[] copyBack = new bool[] { true, true, true, true };
            if (copyBack[0])
            {
                nRow = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[0]), typeof(int));
            }
            if (copyBack[1])
            {
                nCol1 = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[1]), typeof(int));
            }
            if (copyBack[2])
            {
                nRow2 = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[2]), typeof(int));
            }
            if (copyBack[3])
            {
                nCol2 = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[3]), typeof(int));
            }
            CellRange range = (CellRange) NewLateBinding.LateGet(pObjGrid, null, "GetCellRange", arguments, null, null, copyBack);
            range.Style = style;
        }

        public static void SetTitleGrid(object pObjVS)
        {
            int num2 = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.SubtractObject(NewLateBinding.LateGet(NewLateBinding.LateGet(pObjVS, null, "Cols", new object[0], null, null, null), null, "Count", new object[0], null, null, null), 1));
            for (int i = 0; i <= num2; i++)
            {
                object[] arguments = new object[3];
                arguments[0] = 0;
                arguments[1] = i;
                object[] objArray2 = new object[] { 0, i };
                bool[] copyBack = new bool[] { false, true };
                if (copyBack[1])
                {
                    i = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(objArray2[1]), typeof(int));
                }
                arguments[2] = modGeneral.Tranlated(ReturnIfNull(RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(pObjVS, null, "item", objArray2, null, null, copyBack)), "", modGeneral.TypeOfValue.Nothing).ToString(), "");
                NewLateBinding.LateSet(pObjVS, null, "item", arguments, null, null);
            }
        }

        public static void ShowHelp(string TopicName)
        {
            Help.ShowHelp(MyProject.Forms.Frm_main, Application.StartupPath + @"\Help.chm", HelpNavigator.Topic, TopicName);
        }

        public static string[] ShowMultiDropdown(object _flex, int nRow, int nCol, string[] strData, int nNumRow, int nColValue, string strValue)
        {
            MultiColumnDropDown down = new MultiColumnDropDown((C1.Win.C1FlexGrid.C1FlexGrid) _flex, nRow, nCol) {
                DataInit = strData,
                ColValue = nColValue,
                Value = strValue,
                MaxRow = 10
            };
            down.ShowDialog();
            return down.ReturnData;
        }

        public static string[] ShowMultiDropDownBySource(object _flex, int nRow, int nCol, DataTable pDataSource, int nNumRow, int nColValue, string strValue, string strTitle = "")
        {
            MultiColumnDropDown down = new MultiColumnDropDown((C1.Win.C1FlexGrid.C1FlexGrid) _flex, nRow, nCol) {
                ShowTitle = 0
            };
            if (strTitle.Trim() != "")
            {
                string[] strArray = strTitle.Split(new char[] { '|' });
                down.Title = strArray;
            }
            down.DataSource = pDataSource;
            down.ColValue = nColValue;
            down.Value = strValue;
            down.MaxRow = 10;
            down.ShowDialog();
            return down.ReturnData;
        }

        public static void ShowWarning(string str)
        {
            if (Form.ActiveForm != null)
            {
                Form.ActiveForm.Cursor = Cursors.Default;
            }
            MessageBox.Show(str, "DRAGON 2 - Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }

        public static string sLeft(string str, int Lenght)
        {
            return Strings.Left(str, Lenght);
        }

        public static string sMid(string str, int start, int length)
        {
            return Strings.Mid(str, start, length);
        }

        public static string sRight(string str, int Lenght)
        {
            return Strings.Right(str, Lenght);
        }

        public static double SumColGrid(object pObj, int nCol)
        {
            double num3 = 0.0;
            int num4 = Conversions.ToInteger(Microsoft.VisualBasic.CompilerServices.Operators.SubtractObject(NewLateBinding.LateGet(NewLateBinding.LateGet(pObj, null, "Rows", new object[0], null, null, null), null, "Count", new object[0], null, null, null), 1));
            for (int i = Conversions.ToInteger(NewLateBinding.LateGet(NewLateBinding.LateGet(pObj, null, "Rows", new object[0], null, null, null), null, "Fixed", new object[0], null, null, null)); i <= num4; i++)
            {
                object[] arguments = new object[] { i, nCol };
                bool[] copyBack = new bool[] { true, true };
                if (copyBack[0])
                {
                    i = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[0]), typeof(int));
                }
                if (copyBack[1])
                {
                    nCol = (int) Conversions.ChangeType(RuntimeHelpers.GetObjectValue(arguments[1]), typeof(int));
                }
                num3 += Conversions.ToDouble(ReturnIfNull(RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(pObj, null, "Item", arguments, null, null, copyBack)), 0, modGeneral.TypeOfValue.Nothing));
            }
            return num3;
        }

        public static bool TestConnect(string strServer, string strDatabase, string strUserName, string strPassword)
        {
            bool flag=false;
            string connectionString = "";
            connectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" + strUserName.Trim() + ";PWD=" + strPassword.Trim() + ";Initial Catalog=" + strDatabase.Trim() + ";Data Source=" + strServer.Trim() + "";
            try
            {
                OleDbConnection connection = new OleDbConnection(connectionString);
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    connection.Dispose();
                    return true;
                }
                if (modGeneral.H_LANGUAGE == "VIET")
                {
                    ShowWarning("Kết nối kh\x00f4ng th\x00e0nh c\x00f4ng! Bạn phải thiết lập lại th\x00f4ng số kết nối.");
                }
                else
                {
                    ShowWarning("It can not connect to database! Please try again.");
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ShowWarning("Bạn kh\x00f4ng thể kết nối với CSDL v\x00ec th\x00f4ng tin kh\x00f4ng đ\x00fang.");
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public static void WriteValueElementFromXML(string FileName, string Name, string Value)
        {
            if (File.Exists(FileName))
            {
                 IEnumerator enumerator=null;
                XmlDocument document = new XmlDocument();
                document.Load(FileName);
                XmlNodeList elementsByTagName = document.GetElementsByTagName(Name);
                try
                {
                    enumerator = elementsByTagName.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        XmlNode current = (XmlNode) enumerator.Current;
                        current.InnerText = Value;
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
                document.Save(FileName);
            }
        }

        public static string YM2MY(string str)
        {
            if (str.Trim() == "")
            {
                return "";
            }
            return (sRight(str.Trim(), 2) + "/" + sLeft(str, 4));
        }
    }
}

